﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BaseDatos;

namespace SCHD
{
    public partial class WFPerfiles : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        public WFPerfiles()
        {
            InitializeComponent();
        }
        private bool Modificado = false;
        private void TsbSalir_Click(object sender, EventArgs e)
        {
            if (Modificado == true)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                DialogResult = DialogResult.Cancel;
            }
        }

        private void TsbPerfiles_Click(object sender, EventArgs e)
        {
            Modificado = true;
            ControlError.SetError(TxtDescripcion, "");
            if (string.IsNullOrEmpty(TxtDescripcion.Text))
            {
                ControlError.SetError(TxtDescripcion, "Ingrese una Descripción");    
            }
            if (TxtDescripcion.Text.Length > 0)
            {
                string usr = Environment.UserName.ToUpper();
                string wks = Environment.MachineName.ToUpper();
                string SQL = "" +
                " insert into chdperfil (codperf,dscperf,usrcre,fchcre,wkscre,usrmod,fchmod,wksmod,flgeli) " +
                " values ((select lpad(max(codperf)+1,3,'0') from chdperfil),'" + TxtDescripcion.Text + "','" + usr + "',sysdate,'"+wks+"','"+usr+"',sysdate,'"+wks+"','0')";
                ConexionBaseDatos.Conexion.EjecutarComando(SQL, CommandType.Text);
                Cargar();
            }
        }

        private void WFPerfiles_Load(object sender, EventArgs e)
        {
            Cargar();
        }

        protected void Cargar()
        {
            string SQL = "select codperf,dscperf from chdperfil where flgeli ='0' order by codperf";
            DataSet ds = ConexionBaseDatos.Conexion.CrearDataSet(SQL, CommandType.Text);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GrdPerfiles.DataSource = ds.Tables[0];
            }
        }

        private void TsbEliminar_Click(object sender, EventArgs e)
        {
            Modificado = true;
            string codigo = GrdPerfiles.CurrentRow.Cells[0].Value.ToString();
            ConexionBaseDatos.Conexion.EjecutarComando("update chdperfil set flgeli ='1' where codperf = '" + codigo + "'",CommandType.Text);
            Cargar();
        }

        private void TsbModificar_Click(object sender, EventArgs e)
        {
            Modificado = true;
            string cod = GrdPerfiles.CurrentRow.Cells[0].Value.ToString();
            string desc = GrdPerfiles.CurrentRow.Cells[1].Value.ToString();
            WFPerfil_Edit perfil = new WFPerfil_Edit(cod,desc);
            perfil.ShowDialog();
            if (perfil.DialogResult == DialogResult.OK)
            {
                Cargar();
            }
        }

    }
}
